#!/bin/bash
echo "🔧 Workspace文件支持增强方案..."

cat > src/cbp2make_enhanced.cpp << 'ENHANCED_CODE'
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <filesystem>

namespace fs = std::filesystem;

class EnhancedCBParser {
public:
    static bool ParseProjectFile(const std::string& filename, std::string& projectName, std::vector<std::string>& sourceFiles) {
        std::string extension = fs::path(filename).extension().string();
        
        if (extension == ".cbp") {
            return ParseCBPFile(filename, projectName, sourceFiles);
        } else if (extension == ".workspace") {
            return ParseWorkspaceFile(filename, projectName, sourceFiles);
        } else {
            std::cout << "Unsupported file type: " << extension << std::endl;
            return false;
        }
    }
    
private:
    static bool ParseCBPFile(const std::string& filename, std::string& projectName, std::vector<std::string>& sourceFiles) {
        // 现有的CBP解析逻辑
        std::ifstream file(filename);
        if (!file.is_open()) return false;
        
        std::string line;
        while (std::getline(file, line)) {
            if (line.find("<Option title=") != std::string::npos) {
                size_t start = line.find('"');
                size_t end = line.find('"', start + 1);
                if (start != std::string::npos && end != std::string::npos) {
                    projectName = line.substr(start + 1, end - start - 1);
                }
            }
            
            if (line.find("<Unit filename=") != std::string::npos) {
                size_t start = line.find('"');
                size_t end = line.find('"', start + 1);
                if (start != std::string::npos && end != std::string::npos) {
                    std::string filePath = line.substr(start + 1, end - start - 1);
                    if (IsSourceFile(filePath)) {
                        sourceFiles.push_back(filePath);
                    }
                }
            }
        }
        return !projectName.empty();
    }
    
    static bool ParseWorkspaceFile(const std::string& filename, std::string& workspaceName, std::vector<std::string>& projectFiles) {
        std::ifstream file(filename);
        if (!file.is_open()) return false;
        
        std::string line;
        while (std::getline(file, line)) {
            if (line.find("<Workspace title=") != std::string::npos) {
                size_t start = line.find('"');
                size_t end = line.find('"', start + 1);
                if (start != std::string::npos && end != std::string::npos) {
                    workspaceName = line.substr(start + 1, end - start - 1);
                }
            }
            
            if (line.find("<Project filename=") != std::string::npos) {
                size_t start = line.find('"');
                size_t end = line.find('"', start + 1);
                if (start != std::string::npos && end != std::string::npos) {
                    std::string projectFile = line.substr(start + 1, end - start - 1);
                    projectFiles.push_back(projectFile);
                }
            }
        }
        return !workspaceName.empty();
    }
    
    static bool IsSourceFile(const std::string& filePath) {
        std::string ext = fs::path(filePath).extension().string();
        return ext == ".cpp" || ext == ".c" || ext == ".h" || ext == ".hpp";
    }
};
ENHANCED_CODE

echo "✅ 增强版解析器代码已生成"
echo "💡 这个版本可以同时支持 .cbp 和 .workspace 文件"
